﻿//-----------------------------------------------------------------------
// <copyright file="MainPage.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace vsarFlightPlan
{
    using System;
    using Windows.ApplicationModel.DataTransfer;
    using Windows.ApplicationModel.Resources;
    using Windows.Storage.Streams;
    using Windows.System;
    using Windows.UI.Popups;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Input;

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private ViewModels.MainPage _viewModel;
        private Flyout CurrentVisibleFlyOut;
        private DataTransferManager dataTransferManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="MainPage"/> class.
        /// </summary>
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            dataTransferManager.DataRequested -= dataTransferManager_DataRequested;
        }

        protected async override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            this.dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += dataTransferManager_DataRequested;
            if (this._viewModel == null)
            {
                this._viewModel = new ViewModels.MainPage();
                this.DataContext = _viewModel;
            }

            await this._viewModel.Load();
            if (_viewModel.ShowSettingsPrompt)
            {
                var dialog = new MessageDialog("Before we can show you the information, we need the settings");
                dialog.Commands.Add(new UICommand("ok", c =>
                {
                    _viewModel.GoToSettings.Execute(null);
                }));

                await dialog.ShowAsync();
            }
        }

        private async void AppBarSaveButton_Click(object sender, RoutedEventArgs e)
        {
            await _viewModel.SaveVisualElementToFile(this);
        }

        private void Button_Tapped(object sender, TappedRoutedEventArgs e)
        {
            this.CurrentVisibleFlyOut.Hide();
        }

        private async void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var deferral = args.Request.GetDeferral();
            args.Request.Data.Properties.ApplicationName = "Flight Plan";
            args.Request.Data.Properties.Title = "Flight Plan";
            var stream = new InMemoryRandomAccessStream();
            await _viewModel.SaveVisualElementToStream(this, stream);
            args.Request.Data.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
            deferral.Complete();
        }

        private void Flyout_Closed(object sender, object e)
        {
            this.CurrentVisibleFlyOut = null;
        }

        private void Flyout_Opening(object sender, object e)
        {
            this.CurrentVisibleFlyOut = sender as Flyout;
        }

        private async void PrivacyPolicyAppBarButtonClick(object sender, RoutedEventArgs e)
        {
            var resourceLoader = ResourceLoader.GetForCurrentView("LocalisedStrings");
            await Launcher.LaunchUriAsync(new Uri(resourceLoader.GetString("PrivacyPolicyURL")));
        }

        private async void ProvideFeedbackAppBarButtonClick(object sender, RoutedEventArgs e)
        {
            var resourceLoader = ResourceLoader.GetForCurrentView("LocalisedStrings");
            await Launcher.LaunchUriAsync(new Uri(resourceLoader.GetString("FeedbackURL")));
        }

        private void ResetButtonTapped(object sender, TappedRoutedEventArgs e)
        {
            _viewModel.Search = null;
            _viewModel.UpdateDisplayItems();
        }

        private void SettingsAppBarButtonClick(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Settings));
        }

        private async void TextBlock_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var resourceLoader = ResourceLoader.GetForCurrentView("LocalisedStrings");
            await Launcher.LaunchUriAsync(new Uri(resourceLoader.GetString("SolutionsURL")));
        }
    }
}